package com.xy.blog.article.domain.controller;


import com.xy.blog.article.api.ArticleService;
import com.xy.blog.article.api.model.ArticleDTO;
import com.xy.blog.article.api.model.ArticleQuery;
import com.xy.blog.article.domain.mapstruct.ArticleConverter;
import com.xy.blog.article.domain.service.IArticleService;
import com.xy.blog.core.exception.BaseException;
import com.xy.blog.core.springmvc.controller.BaseController;
import com.xy.blog.core.springmvc.result.page.PageReq;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;

/**
 * @author zanhonglei
 * @version V1.0
 * @Description:
 * @date 2019/11/2 14:32
 */
@Api(value = "ArticleController",tags = "文章管理")
@RestController
public class ArticleController extends BaseController implements ArticleService {

    @Autowired
    private IArticleService articleServiceImpl;

    @Autowired
    private ArticleConverter articleConverter;

    /**
     * 分页获取文章
     * @return
     */

    @Override
    @ApiOperation(value = "分页获取文章", notes = "分页获取文章", httpMethod = "POST")
    public Object pages(PageReq<ArticleQuery> req) throws BaseException {
        return getPageData(articleServiceImpl.listArticlesByPage(req));
    }
    /**
     * 根据文章id获取文章
     * @param id
     * @return
     */
    @Override
    @ApiOperation(value = "根据文章id获取文章", notes = "根据文章id获取文章", httpMethod = "GET")
    public Object getById(Long id) throws BaseException {
        return getData(articleConverter.bo2Vo(articleServiceImpl.getArticleById(id)));
    }

    /**
     * 发布文章
     * @param req
     * @return
     */
    @Override
    @ApiOperation(value = "发布文章", notes = "发布文章", httpMethod = "POST")
    public Object save(@Valid ArticleDTO req) throws BaseException {
        articleServiceImpl.add(articleConverter.req2Bo(req));
        return success();
    }

    /**
     * 文章更新
     * @param req
     * @return
     */
    @Override
    @ApiOperation(value = "文章更新", notes = "文章更新", httpMethod = "PUT")
    public Object update(@Valid ArticleDTO req) throws BaseException {
        articleServiceImpl.updateArticle(articleConverter.req2Bo(req));
        return success();
    }
    /**
     * 根据文章id删除文章
     * @param id
     * @return
     */
    @Override
    @ApiOperation(value = "根据文章id删除文章", notes = "根据文章id删除文章", httpMethod = "DELETE")
    public Object deleteById(Long id) throws BaseException {
        articleServiceImpl.deleteArticleById(id);
        return success();
    }

    @Override
    public Object like(Long id) throws BaseException {
        articleServiceImpl.like(id);
        return success();

    }

    @Override
    public Object view(Long id) throws BaseException {
        articleServiceImpl.view(id);
        return success();
    }

    /**
     * 修改文章排序
     * @param oldId
     * @param newId
     * @return
     */
    @Override
    public Object changeOrders(@RequestParam String oldId,@RequestParam String newId) {
        articleServiceImpl.changeOrders(oldId, newId);
        return success();
    }

}
